<--- %%NOBANNER%% --> prop1_ss.sas
 BackForward

/*------------------<--- Start of Description -->--------------------\
| Sample size for one proportion;                                    |
|--------------------<--- End of Description -->---------------------|
|--------------------------------------------------------------------|
|--------------<--- Start of Files or Arguments Needed -->-----------|
| Arguments:                                                         |
|   - Required:                                                      |
|     y1 = true proportion under null hypothesis, 0------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
| Example: %prop1_ss(sides=1,y1=.0004,min_y2=.0005,max_y2=.002,      |
|                    inc_y2=.0001, power=.9,plot=p);                 |
| Usage:   %prop1_ss (ALPHA=.05,SIDES=2,POWER=.90,Y1=.,MIN_Y2=.,     |
|                MAX_Y2=.,INC_Y2=.,PLOT= );                          |
| Reference: Bergstralh, EJ.  SAS macros for sample size and power   |
|            calculations.  Proceedings of the 9th annual SAS Users  |
|            Group International Conference.                         |
|            Equation #'s 9 and 10.                                  |
\-------------------<--- End of Example and Usage -->---------------*/
%MACRO prop1_ss (ALPHA=.05,SIDES=2,POWER=.90,Y1=.,MIN_Y2=.,
                MAX_Y2=.,INC_Y2=.,PLOT= );
/*--------------------------------------------\
| Author:  Michael Riggs and Eric Bergstralh; |
| Purpose: Sample Size for One proportions;   |
\--------------------------------------------*/
 OPTIONS MISSING=' ' NOCENTER;
   %LET PLOT=%UPCASE(&PLOT);
 DATA T1;
      ALPHA=&ALPHA;
      SIDES=&SIDES;
      Y1=&Y1;
      MIN_Y2=&MIN_Y2;
      MAX_Y2=&MAX_Y2;
      INC_Y2=&INC_Y2;
      POWER=&POWER;
    ZALPHA=(PROBIT(1-ALPHA))*(SIDES=1) + (PROBIT(1-ALPHA/2))*(SIDES=2);
    ZBETA=PROBIT(POWER);
    IF MAX_Y2=. THEN DO;
      MAX_Y2=MIN_Y2+1; INC_Y2=MIN_Y2+2;  *NEED 1 EXEC OF DO;
    END;
    TY1=Y1;
    DO Y2=MIN_Y2 TO MAX_Y2 BY INC_Y2;
      RR=Y2/TY1;
      *NORMAL APPROX. TO BINOMIAL;
       N1_NML=( (ZALPHA*SQRT(TY1*(1-TY1))+ZBETA*SQRT(Y2*(1-Y2))) /
               (Y2-TY1) )**2;
       N1_NML=CEIL(N1_NML);
      *NORMAL APPROX. FOLLOWING 2*ARCSIN(SQRT(PROP.)) TRANSFORMATION;
       AS_Y1=2*ARSIN(SQRT(TY1));
       AS_Y2=2*ARSIN(SQRT(Y2));
       N1_ARS=( (ZALPHA+ZBETA)/(AS_Y1-AS_Y2) )**2;
       N1_ARS=CEIL(N1_ARS);
      *---------------------------------------------------;
       OUTPUT;
     END;
 LABEL Y1='Null hyp.*Proportion'
       Y2='Alt. hyp.*Proportion'
       N1_NML='Sample*Size*Binomial @'
       N1_ARS='Sample*Size*Arcsin @@';
RUN;
 PROC PRINT SPLIT='*';
      ID y1;
      VAR  Y2 N1_NML N1_ARS;
            FOOTNOTE1 '@, Normal approximation to binomial.';
            FOOTNOTE2
            '@@, Arcsin transformation followed by normal approximation.';
 TITLE3
       'SAMPLE SIZE REQUIREMENTS FOR ONE PROPORTION';
 title4
"Alpha=&alpha, Sides=&sides, Power=&power, Null hypothesis prop.=&y1";

%IF &MAX_Y2 NE . %THEN %DO;
    %IF &PLOT= P  %THEN %DO;
      PROC PLOT NOLEGEND;
           PLOT N1_NML*Y2='N'  N1_ARS*Y2='A' / OVERLAY;
            LABEL N1_NML='Sample size'
                  Y2='Alt. hypothesis proportion';
            FOOTNOTE1 'N=Normal approximation to binomial';
            FOOTNOTE2
            'A=Arcsin transformation followed by normal approximation';
    %END;
    %ELSE %IF &PLOT= G  %THEN %DO;
          PROC GPLOT ;
           PLOT N1_NML*Y2 N1_ARS*Y2 / OVERLAY;
           SYMBOL1  F=SPECIAL V=J H=1 I=j l=1;
           SYMBOL2  F=SPECIAL V=M H=1   I=j l=2;
            LABEL N1_NML='Sample size'
                  Y2='Alt. hypothesis proportion';
           FOOTNOTE1 F=SPECIAL   ' ' M=(+6,-.7) H=2   'K'
                     F=TRIPLEX  H=1 M=(-.5,+.3)
                     '  Normal approx. to binomial'
                     F=SPECIAL M=(+6 -.4) H=2   'M'
                     F=TRIPLEX  H=1 M=(-.5,+.4)
                     '  Normal approx. with arcsin';
    %END;
    RUN;
%END;
%MEND prop1_ss;